﻿@model Century.Models.Common.SitePage
@{
    ViewBag.Title = "Country Site Script Management";
    TempData["ScriptManagerMode"] = true;
    Layout = "~/Areas/Admin/Views/Shared/_TreeLayout.cshtml";
}
@{
    if (Model != null)
    {
        <br />
        <h2>
            @(!string.IsNullOrEmpty(Model.Name) ? "\""+ Model.Name + "\" Page" : string.Empty) Script Management</h2>
        <br />
        <p>
            <b>Rules : </b>
            <ul>
                <li>- <b>Script :</b> Must be filled.</li>
                <li>- <b>IsActive :</b> If value of this field is true, script will be executed</li>
                <li>- <b>Script Key :</b> Must be filled.</li>
                <li>- <b>Page Name :</b> Don't have to fill it, but if you need a page specific script,
                    you have to fill. (if you filled it, PageName field must be same with LocalPageName)</li>
                <li>- <b>Description :</b> Don't have to fill, but it may be usefull for that regocnize
                    script content</li>
                <li>- <b>Execution Order :</b> If your query execution order is important, you have
                    to fill order field.</li>
                <li>- <b>Render Position :</b> You have to choose where script will be shown. Some scripts
                    needs different positions.</li>
            </ul>
        </p>
        <br />
        @(Html.Telerik().Grid<Century.Models.Common.CountrySiteScript>()
            .Name("Grid")
            .DataKeys(keys => keys.Add(c => c.Id))
            .ToolBar(commands =>
            {
                commands.Insert().ButtonType(GridButtonType.ImageAndText).ImageHtmlAttributes(new { style = "margin-left:0" });
            })
            .Columns(columns =>
            {
                columns.Bound(o => o.Id).Hidden().ReadOnly(true);
                columns.ForeignKey(o => o.CountrySiteId, (System.Collections.IEnumerable)ViewBag.AccessibleSites, "Id", "Name").Sortable(true).Width(150).Title("Country");
                columns.Bound(o => o.Description).EditorTemplateName("MultiLineEditor").Width(250);
                columns.Bound(o => o.ScriptKey).Width(100);
                columns.Bound(o => o.PageName).Width(100);
                columns.ForeignKey(o => o.RenderPosition, (System.Collections.IEnumerable)ViewBag.RenderPosition, "Id", "Name").Sortable(true).Width(150).Title("Render Position");
                columns.Bound(o => o.Script).EditorTemplateName("MultiLineEditor").Width(300);
                //columns.Bound(o => o.ExecutionOrder).EditorTemplateName("Integer").Width(160);
                columns.Bound(o => o.IsActive);
                columns.Command(command =>
                {
                    command.Edit().ButtonType(GridButtonType.Image);
                }
                                ).Width(50);
            })
            .ClientEvents(clientEvents =>
            {
                clientEvents.OnEdit("editClick");
                clientEvents.OnRowDataBound("onRowDataBound");
            }
            )
            .DataBinding(dataBinding => dataBinding.Ajax()
                        .Select("GetPageScripts", "CountrySiteScript")
                        .Update("UpdatePageScripts", "CountrySiteScript")
                        .Insert("InsertPageScripts", "CountrySiteScript")
            )
            .Groupable()
            .Editable(editing => editing.Mode(GridEditMode.PopUp).FormHtmlAttributes(new { style = "width:200px;" }))
            .Pageable(pagingSetting => pagingSetting.PageSize(30))
            //.Sortable(sortable => sortable.OrderBy(order => order.Add(o => o.CountrySite.Path).Descending()))
            .Scrollable(c => c.Height("350px"))
            .Resizable(config =>
            {
                config.Columns(true);
            })
            .Reorderable(config =>
            {
                config.Columns(true);
            })
            .ColumnContextMenu()
        )
    
    }
    else
    {
    <text> Page not found in this site </text>
    }
}

<script type="text/javascript">

    function onRowDataBound(e) {
        // remove edit button for disabled row
        var isPageBind = '@(Model != null && Model.Id > 0)';
     
        if (
            (isPageBind == 'True' && e.dataItem.PageName == null)
            ||
            (isPageBind == 'False' && e.dataItem.PageName != null)
           )
        {
            e.row.style.color = "gray";
            $(e.row).find('a.t-grid-edit').remove();
        }
    }

    function editClick(e) {
        $('.t-html').click('Details', showHtml);
        $(e.form).closest(".t-window-content").width(650).height(650);
        var popup = $("#" + e.currentTarget.id + "PopUp");
        popup.css({ "left": ($(window).width() / 2 - $(popup).width() / 2) + "px", "top": ($(window).height() / 2 - $(popup).height() / 2) + "px" });
        $("#Script").height(220);
    }

    function onDropDownListChange(e) {
        $.ajax({
            type: "post",
            url: '@Url.Action("CountrySiteChange", "CountrySiteSelector")',
            data: { selectedCountrySiteId: e.value
            },
            dataType: "html",
            success: function (msg) {
                $("#Grid").data("tGrid").rebind();
            },
            error: function (msg) {
                alert('Error on changing country site');
            }
        });
    }

</script>